<template>
  <div class="app-container">
    <!-- 查询区域 -->
    <el-card class="box-card" shadow="hover">
      <div slot="header" class="clearfix">
        <span class="card-title">查询条件</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="showSearch = !showSearch">
          {{ showSearch ? '收起' : '展开' }}
        </el-button>
      </div>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="学号" prop="xh">
              <el-input v-model="queryParams.xh" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="入学年度" prop="rxNd">
              <el-input v-model="queryParams.rxNd" placeholder="请输入入学年度" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="班级" prop="bj">
              <el-input v-model="queryParams.bj" placeholder="请输入班级" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="专业" prop="zy">
              <el-select v-model="queryParams.zy" placeholder="请选择专业" clearable style="width: 100%">
                <el-option
                  v-for="dict in dict.type.zy"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="学制" prop="xz">
              <el-select v-model="queryParams.xz" placeholder="请选择学制" clearable style="width: 100%">
                <el-option
                  v-for="dict in dict.type.xz"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="学籍状态" prop="xjZt">
              <el-select v-model="queryParams.xjZt" placeholder="请选择学籍状态" clearable style="width: 100%">
                <el-option
                  v-for="dict in dict.type.xj_zt"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" style="text-align: right">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <!-- 操作按钮和表格区域 -->
    <el-card class="box-card" shadow="hover" style="margin-top: 15px">
      <div class="table-operations">
        <el-button-group>
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd" v-hasPermi="['xsxx:xjxx:add']">新增</el-button>
          <el-button type="success" icon="el-icon-edit" :disabled="single" @click="handleUpdate" v-hasPermi="['xsxx:xjxx:edit']">修改</el-button>
          <el-button type="danger" icon="el-icon-delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['xsxx:xjxx:remove']">删除</el-button>
          <el-button type="warning" icon="el-icon-download" @click="handleExport" v-hasPermi="['xsxx:xjxx:export']">导出</el-button>
        </el-button-group>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </div>
      <!-- 表格区域 -->
      <el-table v-loading="loading" :data="xjxxList" @selection-change="handleSelectionChange" border stripe highlight-current-row style="width: 100%">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="学生姓名" align="center" prop="xxXm" width="120">
          <template slot-scope="scope">
            <el-button type="text" @click="handleViewStudent(scope.row)">{{ scope.row.xxXm }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="学号" align="center" prop="xh" />
        <el-table-column label="入学年度" align="center" prop="rxNd" />
        <el-table-column label="班级" align="center" prop="bj" />
        <el-table-column label="专业" align="center" prop="zy">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.zy" :value="scope.row.zy"/>
          </template>
        </el-table-column>
        <el-table-column label="学制" align="center" prop="xz">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.xz" :value="scope.row.xz"/>
          </template>
        </el-table-column>
        <el-table-column label="学籍状态" align="center" prop="xjZt">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.xj_zt" :value="scope.row.xjZt"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['xsxx:xjxx:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['xsxx:xjxx:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    </el-card>

    <!-- 弹窗区域 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-card class="box-card">
          <div slot="header" class="clearfix" style="text-align: center">
            <span style="font-size: 20px;color: #1890ff">学籍基本信息</span>
          </div>
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="学生姓名" prop="xxXm">
                <el-input
                  v-model="form.xxXm"
                  :placeholder="form.xjxjId ? '' : '请选择学生'"
                  readonly
                  :disabled="form.xjxjId != null"
                  @click.native="form.xjxjId == null && (showStudentDialog = true)"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="学号" prop="xh">
                <el-input v-model="form.xh" placeholder="请输入学号" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="入学年度" prop="rxNd">
                <el-input v-model="form.rxNd" placeholder="请输入入学年度" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="班级" prop="bj">
                <el-input v-model="form.bj" placeholder="请输入班级" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="专业" prop="zy">
                <el-select v-model="form.zy" placeholder="请选择专业" style="width: 100%">
                  <el-option
                    v-for="dict in dict.type.zy"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="学制" prop="xz">
                <el-select v-model="form.xz" placeholder="请选择学制" style="width: 100%">
                  <el-option
                    v-for="dict in dict.type.xz"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="学籍状态" prop="xjZt">
            <el-select v-model="form.xjZt" placeholder="请选择学籍状态" style="width: 100%">
              <el-option
                v-for="dict in dict.type.xj_zt"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
      <!-- 嵌入学生选择弹窗 -->
      <XsXxDx :visible.sync="showStudentDialog" @select="handleStudentSelect" />
    </el-dialog>

    <!-- 添加学生基本信息查看对话框 -->
    <el-dialog :title="'学生基本信息'" :visible.sync="studentDialogVisible" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="姓名">{{ studentInfo.xm }}</el-descriptions-item>
        <el-descriptions-item label="学号">{{ studentInfo.xh }}</el-descriptions-item>
        <el-descriptions-item label="性别">
          <dict-tag :options="dict.type.sys_user_sex" :value="studentInfo.xb"/>
        </el-descriptions-item>
        <el-descriptions-item label="出生日期">{{ parseTime(studentInfo.scRq, '{y}-{m}-{d}') }}</el-descriptions-item>
        <el-descriptions-item label="民族">{{ studentInfo.mz }}</el-descriptions-item>
        <el-descriptions-item label="政治面貌">{{ studentInfo.zzMm }}</el-descriptions-item>
        <el-descriptions-item label="身份证号">{{ studentInfo.sfZh }}</el-descriptions-item>
        <el-descriptions-item label="联系方式">{{ studentInfo.lxFs }}</el-descriptions-item>
        <el-descriptions-item label="家庭住址" :span="2">{{ studentInfo.jtZz }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="studentDialogVisible = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 新增学籍详情弹窗 -->
    <el-dialog :title="'学籍详情'" :visible.sync="detailDialogVisible" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="学生姓名">{{ detailInfo.xxXm }}</el-descriptions-item>
        <el-descriptions-item label="学号">{{ detailInfo.xh }}</el-descriptions-item>
        <el-descriptions-item label="入学年度">{{ detailInfo.rxNd }}</el-descriptions-item>
        <el-descriptions-item label="班级">{{ detailInfo.bj }}</el-descriptions-item>
        <el-descriptions-item label="专业">{{ detailInfo.zy }}</el-descriptions-item>
        <el-descriptions-item label="学制">{{ detailInfo.xz }}</el-descriptions-item>
        <el-descriptions-item label="学籍状态">{{ detailInfo.xjZt }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailDialogVisible = false">关 闭</el-button>
      </div>
    </el-dialog>

  </div>

</template>

<style lang="scss" scoped>
.app-container {
  padding: 20px;

  .box-card {
    .card-title {
      font-size: 16px;
      font-weight: bold;
      color: #303133;
    }
  }

  .table-operations {
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .el-button-group {
    .el-button {
      margin-right: 0;
    }
  }

  .el-table {
    margin: 15px 0;
  }

  .el-form {
    .el-form-item {
      margin-bottom: 22px;
    }
  }

  .el-dialog {
    .el-dialog__body {
      padding: 20px 30px;
    }
  }
}

.el-input.is-readonly .el-input__inner {
  background-color: #f5f7fa;
  cursor: pointer;
}
</style>

<script>
import { listXjxx, getXjxx, delXjxx, addXjxx, updateXjxx } from "@/api/xsxx/xjxx"
import { getJbxx } from "@/api/xsxx/jbxx"
import XsXxDx from "@/views/components/xsxxxz/XsXxDx.vue";
import { getToken } from '@/utils/auth';

export default {
  name: "Xjxx",
  components: {XsXxDx},
  dicts: ['zy', 'xj_zt', 'xz', 'sys_user_sex', 'sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学籍信息表格数据
      xjxxList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      showStudentDialog: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jbxxId: null,
        xh: null,
        rxNd: null,
        bj: null,
        zy: null,
        xz: null,
        xjZt: null,
        userId: null,
        deptId: null
      },
      // 表单参数
      form: {
        xjxxId: null,
        jbxxId: null,
        xxXm: null,
        xh: null,
        rxNd: null,
        bj: null,
        zy: null,
        xz: null,
        xjZt: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null
      },
      // 表单校验
      rules: {
        xxXm: [
          { required: true, message: "请选择学生", trigger: "change" }
        ],
        xh: [
          { required: true, message: "学号不能为空", trigger: "blur" }
        ],
        rxNd: [
          { required: true, message: "入学年度不能为空", trigger: "blur" }
        ],
        bj: [
          { required: true, message: "班级不能为空", trigger: "blur" }
        ],
        zy: [
          { required: true, message: "专业不能为空", trigger: "change" }
        ],
        xz: [
          { required: true, message: "学制不能为空", trigger: "change" }
        ],
        xjZt: [
          { required: true, message: "学籍状态不能为空", trigger: "change" }
        ],

      },
      // 选中的学生信息
      selectedStudent: null,
      // 学生基本信息对话框
      studentDialogVisible: false,
      // 学生基本信息
      studentInfo: {},
      detailDialogVisible: false,
      detailInfo: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询学籍信息列表 */
    getList() {
      this.loading = true
      listXjxx(this.queryParams).then(response => {
        const promises = response.rows.map(item => {
          if (item.jbxxId) {
            return getJbxx(item.jbxxId).then(res => {
              item.xxXm = res.data.xm
              return item
            })
          }
          return Promise.resolve(item)
        })
        
        Promise.all(promises).then(results => {
          this.xjxxList = results
          this.total = response.total
          this.loading = false
        })
      })
    },
    /** 处理学生选择 */
    handleStudentSelect(student) {
      if (student) {
        this.form.jbxxId = student.jbxxId
        this.form.xxXm = student.xm
        this.form.xh = student.xh
        this.showStudentDialog = false
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.selectedStudent = null;
    },
    // 表单重置
    reset() {
      this.form = {
        xjxxId: null,
        jbxxId: null,
        xm: null,
        xh: null,
        rxNd: null,
        bj: null,
        zy: null,
        xz: null,
        xjZt: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null,
        xxXm:null,
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.xjxjId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加学籍信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const xjxjId = row.xjxjId || this.ids
      getXjxx(xjxjId).then(response => {
        this.form = response.data
        // 获取学生信息并赋值学生姓名
        if (this.form.jbxxId) {
          getJbxx(this.form.jbxxId).then(res => {
            this.form.xxXm = res.data.xm
            this.selectedStudent = res.data
            this.open = true
            this.title = "修改学籍信息"
          })
        } else {
          this.open = true
          this.title = "修改学籍信息"
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.xjxjId != null) {
            updateXjxx(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addXjxx(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const xjxjIds = row.xjxjId || this.ids
      this.$modal.confirm('是否确认删除学籍信息编号为"' + xjxjIds + '"的数据项？').then(function() {
        return delXjxx(xjxjIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 查看学生基本信息 */
    handleViewStudent(row) {
      if (row.jbxxId) {
        getJbxx(row.jbxxId).then(response => {
          if (response.data) {
            this.studentInfo = {
              ...response.data,
              xh: row.xh  // 使用学籍信息中的学号
            };
            this.studentDialogVisible = true;
          }
        });
      }
    },
    handleDetail(row) {
      getXjxx(row.xjxjId).then(res => {
        this.detailInfo = res.data;
        this.detailDialogVisible = true;
      });
    },
  }
}
</script>
